
package com.compuvest.web;

// <editor-fold defaultstate="collapsed" desc="import">
import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
// </editor-fold>

public class Basket implements Serializable {

// <editor-fold defaultstate="collapsed" desc="init">

    public static final double PI_TRANS_FEE = 0.045d;	// 4.5%
//    public static final double WA_TAX_FEE = 0.095d; // WA tax 9.5%
//    public static final double COD_FEE = 7.5d;		// $

    private HashMap<Integer,ItemsInBasket> contents;
    private Customer customer;
    private String paymentMethod = "";
    private double shippingCost = 0d;
    private String shipCode = "";
    private String shipDesc = "";
    private double grandTotal = 0d;
    private double transFee = 0d;
    private int orderid = 0;
    private String orderNumber = "0";
    private String orderDate = "";
    private String notes = "";
    private boolean reseller = false;
    private boolean blindShipping = false;
    private boolean packingSlip = false;
    private String purchaseOrder = "";
    private Clickstream clickstream;
    private CookieBean cookieBean;
// </editor-fold>

// <editor-fold defaultstate="collapsed" desc="Basket()">

    public Basket() {
	contents = new HashMap<>();
	clickstream = new Clickstream();
    }
// </editor-fold>

// <editor-fold defaultstate="collapsed" desc="[gs]set">

    public Customer getCustomer()                       { return customer; }
    public void setCustomer(Customer customer)          { this.customer = customer; }

    public boolean getReseller()                        { return reseller; }
    public void setReseller(boolean reseller)           { this.reseller = reseller; }

    public boolean getBlindShipping()			{ return blindShipping; }
    public void setBlindShipping(boolean blindShipping)	{ this.blindShipping = blindShipping; }

    public boolean getPackingSlip()			{ return packingSlip; }
    public void setPackingSlip(boolean packingSlip)	{ this.packingSlip = packingSlip; }

    public String getPurchaseOrder()			{ return purchaseOrder; }
    public void setPurchaseOrder(String purchaseOrder)	{ this.purchaseOrder = purchaseOrder; }

    public String getPaymentMethod()			{ return paymentMethod; }
    public void setPaymentMethod(String paymentMethod)	{ this.paymentMethod = paymentMethod; }

    // Paily transaction fee
    public void setTransFee(double transFee)            { this.transFee = transFee; }
    public double getTransFee()                         { return transFee; }

    public double getShippingCost()			{ return shippingCost; }
    public void setShippingCost(double shippingCost)	{ this.shippingCost = shippingCost; }

    public String getShippingCode()			{ return shipCode; }
    public void setShippingCode(String shipCode)	{ this.shipCode = shipCode; }

    public String getShippingDescription()		{ return shipDesc; }
    public void setShippingDescription(String shipDesc)	{ this.shipDesc = shipDesc; }

    public int getOrderID()				{ return orderid; }
    public void setOrderID(int orderid)                 { this.orderid = orderid; }

    public String getOrderNumber()			{ return orderNumber; }
    public void setOrderNumber(String orderNumber)	{ this.orderNumber = orderNumber; }

    public String getOrderDate()			{ return orderDate; }
    public void setOrderDate(String orderDate)          { this.orderDate = orderDate; }

    public String getNotes()				{ return notes; }
    public void setNotes(String notes)			{ this.notes = notes; }

    // for cookie advert onetime
    public void setCookies(CookieBean cookieBean)	{ this.cookieBean = cookieBean; }
    public CookieBean getCookieBean()			{ return cookieBean; }

    // for statistic
    public void setClickstream(Clickstream clickstream)	{ this.clickstream = clickstream; }
    public Clickstream getClickstream()			{ return clickstream; }

    public ArrayList<ItemsInBasket> getContents() {
	return new ArrayList<>(contents.values());
    }
// </editor-fold>

// <editor-fold defaultstate="collapsed" desc="addItem()">

    public void addItem(ItemsInBasket item) {

	int itemID = item.getItemID();
	if(contents.containsKey(itemID)) {
            ItemsInBasket itemInCart = (ItemsInBasket)contents.get(itemID);
            item.setTotalQuantity(item.getTotalQuantity() + itemInCart.getTotalQuantity());

            if( !item.getStock()) { // test on no more quantity
                if(item.getQuantity() < item.getTotalQuantity()) {
                    item.setTotalQuantity(item.getQuantity());
                }
            }
	}
	contents.put(itemID, item);
    }
// </editor-fold>

// <editor-fold defaultstate="collapsed" desc="setQuantity()">

    public void setQuantity(int itemID, int quantity, String warranty) {

	if(!contents.containsKey(itemID)) { return; }
	else if(quantity <= 0) {
            contents.remove(itemID);
            return;
	}

	ItemsInBasket item = (ItemsInBasket)contents.get(itemID);
	if(!item.getStock()) { // Check quantity on MAXimum
            if(item.getQuantity() < quantity) {
                quantity = item.getQuantity();
            }
        }
        else { // check hackers(no more than 1000 ea.)
            if(999 < quantity) {
                quantity = 999;
            }
        }
        item.setTotalQuantity(quantity);

        // set warranty
        if(warranty != null) {
            int pos = warranty.indexOf('|');
            if(pos != -1) {
                double warrantyPrice = 0d;
                try { warrantyPrice = Double.parseDouble(warranty.substring(pos + 1, warranty.length())); } catch(Exception ignore) { }

                item.setWarrantyPrice(warrantyPrice);
                item.setWarrantyDesc(warranty.substring(0, pos));
            }
        }
    }
// </editor-fold>

// <editor-fold defaultstate="collapsed" desc="getTotal()">

    public double getTotal() {

        double total = 0d;
        ItemsInBasket item;
        for(int itemid : contents.keySet()) {
            item = contents.get(itemid);
            total += (double)item.getTotalQuantity() * item.getSellPrice() + item.getWarrantyTotal();
        }
	return total;
    }
// </editor-fold>

    public double getTempTotal() {	  // COD
//	return getTotal() + shippingCost + ("CD".equals(paymentMethod) ? COD_FEE : 0d);
	return getTotal() + shippingCost;
    }

    public double getGrandTotal()                   { return grandTotal; }
    public void setGrandTotal(double grandTotal)    { this.grandTotal = grandTotal; }

// <editor-fold defaultstate="collapsed" desc="getTotalWeight()">

    public double getTotalWeight() {

        double totalWeight = 0d;
        ItemsInBasket item;
        for(int itemid : contents.keySet()) {
            item = contents.get(itemid);
            totalWeight += (double)item.getTotalQuantity() * item.getWeight();
        }
        return totalWeight;
    }
// </editor-fold>

// <editor-fold defaultstate="collapsed" desc="size()">

    public int size()	{ return contents.size(); }
// </editor-fold>

// <editor-fold defaultstate="collapsed" desc="clean()">

    public void clean()	{ 
	contents.clear();
	this.paymentMethod = "";
	this.shippingCost = 0d;
	this.grandTotal = 0d;
	this.reseller = false;
    }
// </editor-fold>

// <editor-fold defaultstate="collapsed" desc="toString()">

    @Override
    public String toString()	{ 
        StringBuilder sb = new StringBuilder();

        sb.append("\n----start customer----");
        sb.append(customer.toString());
        sb.append("\n----end customer----");
        sb.append("\nreseller: ").append(reseller);
        sb.append("\nblindShipping: ").append(blindShipping);
        sb.append("\npackingSlip: ").append(packingSlip);
        sb.append("\npurchaseOrder: ").append(purchaseOrder);
        sb.append("\npaymentMethod: ").append(paymentMethod);
        sb.append("\ntransFee: ").append(transFee);
        sb.append("\nshippingCost: ").append(shippingCost);
        sb.append("\nshippingCode: ").append(shipCode);
        sb.append("\nshipDesc: ").append(shipDesc);
        sb.append("\norderID: ").append(orderid);
        sb.append("\norderNumber: ").append(orderNumber);
        sb.append("\norderDate: ").append(orderDate);
        sb.append("\nnotes: ").append(notes);
        sb.append("\n----cookie---").append(cookieBean.toString());
        sb.append("\n----click----").append(clickstream.toString());
        sb.append("\n----contens----\n").append(contents.toString());

        return sb.toString();
    }
// </editor-fold>

}
